package com.leonardo.persistenceme.model.persistancehandlers;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public abstract class ArrayPersistenceHandler extends PersistenceHandler {

	protected PersistenceHandler primitiveHandler;
	
	public ArrayPersistenceHandler(PersistenceHandler primitiveHandler) {
		this.primitiveHandler = primitiveHandler;
	}
	
	public void persist(Object obj, DataOutputStream stream) throws IOException {
		if(obj == null) {
			stream.writeInt(0);
			return;
		}
		Object[] array = (Object[]) obj;
		stream.writeInt(array.length);
		for(int i = 0; i < array.length; i++)
			primitiveHandler.persist(array[i], stream);
	}

	public abstract Object recover(DataInputStream stream) throws IOException;
}
